<?php

/**
 * 项目成员
 */
namespace app\api\modules\v1\market\controllers;

use yii\web\Controller;
use app\api\components\ApiController;
use Yii;
use app\models\base\User;
use app\models\base\ProjectMember;
use yii\db\Query;
use app\api\modules\v1\market\models\AppUser;
use app\api\modules\v1\market\models\Message;
use app\api\modules\v1\market\models\Member;
use app\models\base\Project;
use app\models\base\Role;

class MemberController extends ApiController
{
	/**
	 * [actionAllMember 新建项目全部成员]
	 * @return [type] [返回成员列表]
	 */
	public function actionAllMember()
	{
		$user_list = (new Query())->select(['user_id','name','email','role'])->from(User::tableName().' AS user')->where(['disabled'=>0])->all();

		if(!$user_list) return parent::sendMessageJson('暂无成员,请先创建成员',self::$CODE_ERR,$user_list);

		//格式化角色名称
		$user_list = Member::getrolename($user_list);
		//全部角色名称
		$role = Role::find()->where(['disabled'=>0])->all();
		// var_dump($user_list);exit;
		return parent::sendMessageJson(Message::SUCCESS,self::$CODE_SUC,['user_list'=>$user_list,'role_list'=>$role]);
	}

	/**
	 * [actionAddMember 项目已选择的成员]
	 * @return [type] [返回成员列表]
	 */
	public function actionHasMember()
	{
		//获取传递过来的项目id
		$project_id = Yii::$app->request->get('project_id');
		$user_id = Yii::$app->request->get('user_id');

		//不存在项目id
		if(!$project_id) return parent::sendMessageJson('项目id不能为空');
		//判断是否有新增权限
		$project = (new Query())->select('create_user_id')->from(Project::tableName().' AS pro')->where(['project_id'=>$project_id,'disabled'=>0])->one();
		if($project['create_user_id'] == $user_id){
			$disabled = 1;
		}else{
			$disabled = 0;
		}

		$user_list = (new Query())->select(['user.user_id','user.name','user.email','pm.role_id','role.name as r_name'])
							->from(User::tableName().' AS user')
							->where(['user.disabled'=>0])
							->innerJoin(ProjectMember::tableName().' AS pm','pm.user_id=user.user_id')
							->innerJoin(Role::tableName().' AS role','role.id=pm.role_id')
							->andWhere(['pm.project_id'=>$project_id])
							->andWhere(['role.disabled'=>0])
							->all();
		//判断成员有没有活动
		$user_list = Member::haveactivity($user_list,$project_id);
		//全部角色名称
		$role = Role::find()->where(['disabled'=>0])->all();

		// var_dump($user_list->createCommand()->getsql());exit;
		return parent::sendMessageJson(Message::SUCCESS,self::$CODE_SUC,['user_list'=>$user_list,'disabled'=>$disabled,'role_list'=>$role]);
	}

	/**
	 * [actionOtherMember 项目还未添加成员]
	 * @return [type] [成员列表]
	 */
	public function actionOtherMember()
	{
		//获取传递过来的项目id
		$project_id = Yii::$app->request->get('project_id');

		//不存在项目id
		if(!$project_id) return parent::sendMessageJson('项目id不能为空');

		//查询出项目已有成员
		$query = (new Query())->select('user_id')->from(ProjectMember::tableName().' AS pm')->where(['pm.project_id'=>$project_id]);
		
		//子查询 项目还未添加成员
		$user_list = (new Query())->select(['user.user_id','user.name','user.email','user.role','role.name as r_name'])
		->from(User::tableName().' AS user')
		->where(['user.disabled'=>0])
		->innerJoin(Role::tableName().' AS role','role.id=user.role')
		->andWhere(['not in','user_id',$query])
		->andWhere(['role.disabled'=>0])
		->all();
		//全部角色名称
		$role = Role::find()->where(['disabled'=>0])->all();

		return parent::sendMessageJson(Message::SUCCESS,self::$CODE_SUC,['user_list'=>$user_list,'role_list'=>$role]);
	}

	/**
	 * [actionAddMember 添加成员]
	 * @return [type] [true or false]
	 */
	public function actionAddMember()
	{
		$post = Yii::$app->request->post();

		// var_dump($post);exit;
		//验证token
        $verifyToken = new AppUser();

        if($verifyToken->verifyAuth($post['user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');

        //验证成员是否为空
        if(!$post['member_id']) return parent::sendMessageJson('请选择添加的成员');

        //添加成员
        return Member::addmember($post['member_id'],$post['project_id'],$post['user_id'],$post['status'],$post['role_id']);
	}

	/**
	 * [actionAddMember 修改成员角色]
	 * @return [type] [true or false]
	 */
	public function actionUpdate()
	{
		$get = Yii::$app->request->get();
		$project_member = ProjectMember::findOne(['user_id'=>$get['user_id'],'project_id'=>$get['project_id']]);
		$project_member->role_id = $get['role_id'];
		if($project_member->save()){
			return parent::sendMessageJson('修改角色成功',parent::$CODE_SUC);
		}else{
			return parent::sendMessageJson('修改角色失败');
		}
	}

	/**
	 * [actionDelete 删除成员]
	 * @return [type] [true or false]
	 */
	public function actionDelete()
	{
		$post = Yii::$app->request->post();

		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($post['user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');

        //判断是否有删除权限
		$project = (new Query())->select('create_user_id')->from(Project::tableName().' AS pro')->where(['project_id'=>$post['project_id'],'disabled'=>0])->one();
		
		if($project['create_user_id'] == $post['user_id']){
			return Member::delete($post);
		}else{
			return parent::sendMessageJson('您没有删除的权限');
		}

	}
}